package com.lyfei.ydc.api;

import com.lyfei.ydc.domain.dto.TokenDto;
import com.lyfei.ydc.domain.dto.WxLoginRequestDto;
import com.lyfei.ydc.service.WxAuthenticationService;
import com.lyfei.ydc.utils.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/token")
public class TokenController {
    @Autowired
    private WxAuthenticationService wxAuthenticationService;

    @PostMapping("")
    public Map<String, String> getToken(@RequestBody WxLoginRequestDto wxLoginRequest) {
        String code = wxLoginRequest.getCode();
        Map<String, String> map = new HashMap<>();
        String token = wxAuthenticationService.code2Session(code, wxLoginRequest.getUserInfo());

        map.put("token", token);
        return map;
    }

    @PostMapping("/wxLogin")
    public Map<String, String> login(@RequestBody WxLoginRequestDto wxLoginRequest) {
        String code = wxLoginRequest.getCode();
        Map<String, String> map = new HashMap<>();
        String token = wxAuthenticationService.code2Session(code, wxLoginRequest.getUserInfo());

        map.put("token", token);
        return map;
    }

    @PostMapping("/verify")
    public Map<String, Boolean> verify(@RequestBody TokenDto token) {
        Map<String, Boolean> map = new HashMap<>();
        Boolean valid = JwtToken.verifyToken(token.getToken());
        map.put("is_valid", valid);
        return map;
    }
}
